﻿using Auth.Application.IServices.System;
using Auth.Dtos.System;
using Auth.Entities.System;
using Auth.IRepository;
using Auth.Models;
using Auth.Models.System;
using AutoMapper;
using Dapper.Contrib.Plus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Auth.Application.Services.System
{
    public class ApiService : IApiService
    {
        /// <summary>
        /// 工作单元
        /// </summary>
        private readonly IUnitOfWork UnitOfWork;

        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper _mapper;

        public ApiService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            UnitOfWork = unitOfWork;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增数据字典
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<Guid?> InsertAsync(ApiDto dto)
        {
            Api entity = _mapper.Map<Api>(dto);

            // 判断Value是否已存在
            if (await UnitOfWork.Context.HasValueAsync<Api>(nameof(entity.Name), entity.Name))
            {
                return null;
            }
            else
            {
                await UnitOfWork.Context.InsertAsync(entity);
                return entity.Id;
            }
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(ApiDto dto)
        {
            Api entity = _mapper.Map<Api>(dto);
            bool result = await UnitOfWork.Context.DeleteAsync(entity);

            return result;
        }

        /// <summary>
        /// 更新数据字典
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ApiDto dto)
        {
            Api entity = _mapper.Map<Api>(dto);
            bool result = await UnitOfWork.Context.UpdateAsync(entity);

            return result;
        }

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiDto> GetAsync(Guid id)
        {
            Api entity = await UnitOfWork.Context.GetAsync<Api>(id);
            ApiDto dto = _mapper.Map<ApiDto>(entity);

            return dto;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task<PagedList<ApiDto>> GetPagedListAsync(ApiModel model)
        {
            IEnumerable<Api> entities = await UnitOfWork.Context.GetPagedListAsync<Api, ApiModel>(model.Pagination.Page, model.Pagination.ItemsPerPage, model);
            long count = await UnitOfWork.Context.CountAsync<Api, ApiModel>(model);

            IEnumerable<ApiDto> list = _mapper.Map<IEnumerable<ApiDto>>(entities);
            return new PagedList<ApiDto> { List = list, Count = count };
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ApiDto>> CountAsync(ApiModel model)
        {
            IEnumerable<Api> entities = await UnitOfWork.Context.GetListAsync<Api, ApiModel>(model);
            IEnumerable<ApiDto> list = _mapper.Map<IEnumerable<ApiDto>>(entities);

            return list;
        }
    }
}
